package com.lq.ecoldchain.ui.order

import android.arch.lifecycle.MutableLiveData
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.entity.OrderDetailBean
import com.lq.ecoldchain.entity.RecommendOrder
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.MD5
import com.lq.ecoldchain.util.toast

/*
*2019/1/11 0011  16:25
*function by lq
*/
class PrepareViewModel : BaseViewModel() {
    val password by lazy { MutableLiveData<String>() }
    val orderId by lazy { MutableLiveData<String>() }
    val free by lazy { MutableLiveData<Double>() }
    val bean by lazy { MutableLiveData<OrderDetailBean>() }

    fun toPay() {
        if (orderId.value.isNullOrEmpty() || password.value.isNullOrEmpty()) {
            toast { "请完善信息" }
        } else
            BaseNetCore.ApiServiceImpl.takeOrder(orderId.value!!, MD5.encode(password.value), free.value.toString()).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<String>() {
                override fun onSuccess(t: String?) {
                    //popupWindow 判断微信支付还是支付宝支付
                }

                override fun onFail(code: String) {}
            })
    }


    fun orderDetail(id: Int) {
        orderId.value = id.toString()
        BaseNetCore.ApiServiceImpl.orderDetail(id).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<OrderDetailBean>() {
            override fun onSuccess(t: OrderDetailBean?) {
                bean.value = t

                if(t!=null){
                    var money:Double
                    when(t.paymentMethod){
                        1->{//在线付款  订单费用 服务费 保证金
                            money = t.adjustPrice + t.driverPayServiceFee+ t.bond
                            if(t.type==2){//转介绍单  转介绍费
                                money +=t.introduceFee
                            }
                            free.value = money
                        }
                        2->{//到付
                            money = t.driverPayServiceFee + t.bond
                            if(t.type==2){
                                money +=t.introduceFee
                            }
                            free.value = money
                        }
                        3->{//其他

                        }
                    }


                }

            }

            override fun onFail(code: String) {}
        })
    }
}